package com.hk.controller;

import com.hk.compoent.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yellow Docter
 * @date 2023 -10-09
 * @desc
 *    1、可以通过@Value的方式获取配置文件中的数据
 *    2、通过Environment 对象 environment.getProperty("配置文件中的key", String.class);
 */
@RestController
public class Demo01Controller {

    //spring 进行 复制操作 (DI 依赖注入操作)
    // @Value 基本数据类型和 string的赋值方式
    // @Autowired   自动装配
    //     从spring容器中获取对象(必须从spring中的对象) 给 属性对象进行赋值

    @Value("${server.port}")
    private Integer port;

    @Value("${stu.name}")
    private String name;

    @Value("${stu.age}")
    private Integer age;

    @RequestMapping("/read1")
    public String read1(){

        System.out.println("port = " + port);
        System.out.println("name = " + name);
        System.out.println("age = " + age);

        return "read data";
    }

    // 注入环境对象
    @Autowired
    private Environment environment;

    @RequestMapping("/read2")
    public String read2(){

        String name = environment.getProperty("stu.name", String.class);
        System.out.println("name = " + name);

        Integer age = environment.getProperty("stu.age", Integer.class);
        System.out.println("age = " + age);

        return "read data";
    }

}
